﻿using Core.Domain;
using Core.DomainServices;
using Infrastructure.DomainServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;

namespace XMLViewer
{
    /// <summary>
    /// This is a sample parse all the xml files from a specific folder using HtmlAgilityPack.1.4.9 library. 
    /// If you have any questions let me know, my email: bin.nguyen.van@gmail.com
    /// </summary>
    public partial class frmMain : Form
    {
        private IDataRepository repository;
        private IDataParser xmlParser;
        private IBodyParser<HtmlNodeCollection> bodyParser;
        private IHeaderParser<HtmlNodeCollection> headerParser;
        private ILoggerService logger;
        private IList<Core.Domain.FileInfo> files;
        public frmMain()
        {
            InitializeComponent();

            gridFunctionalStatuses.DataBindingComplete += gridDataSouce_DataBindingComplete;
            
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            logger = new FileLogger();
            repository = new DataRepository(logger);
            bodyParser = new BodyParser();
            headerParser = new PatientParser();
            xmlParser = new FileParser(bodyParser, headerParser);
            string path = Directory.GetCurrentDirectory().Replace("bin" + Path.DirectorySeparatorChar + "Debug", "DataSample/18-0869_ACS,1") + Path.DirectorySeparatorChar;
            txtFileName.Text = path;

            files = repository.GetFiles(path);
            BindData(txtFileName.Text);

        }

        private void BindData(string path)
        {

            cboSheetNames.DisplayMember = "FileName";
            cboSheetNames.ValueMember = "Path";
            cboSheetNames.DataSource = files;
            var data = xmlParser.Parser(cboSheetNames.SelectedValue.ToString());

            gridFunctionalStatuses.DataSource = data.FunctionalStatuses;
            gridFamilyHistory.DataSource = data.FamilyHistorys;
            gridProblems.DataSource = data.Problems;
            gridSocialHistory.DataSource = data.SocialHistorys;
            gridMedication.DataSource = data.Medications;
            txtFirstName.Text = data.Patient.FirstName;
            txtLastName.Text = data.Patient.LastName;
            txtGender.Text = data.Patient.Gender;
            txtDOB.Text = data.Patient.DOB;
        }

        private void cboFileNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData(cboSheetNames.SelectedValue.ToString());
        }

        void btnSelectedFile_Click(object sender, System.EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {

                txtFileName.Text = folderBrowserDialog1.SelectedPath;
                files = repository.GetFiles(txtFileName.Text);
                BindData(txtFileName.Text);
            }
        }

        protected void gridDataSouce_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            lblTotal.Text = "Total items: " + files.Count;
        }
    }
}
